/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Vue;

import Modele.ModeleImage;
import Modele.Repertoire;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author BENNIS Wiam
 * Modify by Soufiane GARCH
 */
public class VueDiaporama extends JFrame implements ActionListener{
    
    private ArrayList array;
    private int index = 0;
    private boolean play_pause;
    private Thread thread = null;
        
    private JPanel bouttons;
    private VueImage vI;
    private Container cont;
    
    private RoundButton suivant;
    private RoundButton precedent;
    private RoundButton quitter;
    private RoundButton play;
    //private JButton pause;
    
    
    public VueDiaporama(ArrayList al){
        this.array = new ArrayList(al);
        this.setFrame();
    }
    
    private void setFrame(){
        /*
        GraphicsDevice myDevice = java.awt.GraphicsEnvironment.
        getLocalGraphicsEnvironment().getDefaultScreenDevice();
        Window myWindow = new Window(this);
        myDevice.setFullScreenWindow(myWindow);
        */
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        ModeleImage im = (ModeleImage) array.get(index);
        this.setTitle("Pikasial - Diporama : " + im.getNom());
        this.setBackground(Color.BLACK);
        
        play_pause = false;
        
        cont = this.getContentPane();
        cont.setLayout(null);
        cont.setBackground(Color.BLACK);
        
        this.setButton();
        
        cont.add(bouttons);
        
        cont.validate();
        //this.pack();
        
        this.afficheImage((ModeleImage) array.get(index));
        
        this.setVisible(true);
    }
    
    private void setButton(){
        bouttons = new JPanel();
        bouttons.setLayout(new GridLayout(1,4));
        bouttons.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        bouttons.setBounds(1120, 5, 140, 40);
        bouttons.setBackground(Color.BLACK);
        
        suivant = new RoundButton(new ImageIcon("image\\suivant.gif"));
        suivant.setToolTipText("Image suivante");
        suivant.addActionListener(this);
        
        precedent = new RoundButton(new ImageIcon("image\\precedent.gif"));
        precedent.addActionListener(this);
        precedent.setToolTipText("Image précedente");
        /*
        play = new RoundButton(new ImageIcon("image\\play.gif"));
        play.setToolTipText("Jouer en continu");
        play.addActionListener(this);
        */
        quitter = new RoundButton(new ImageIcon("image\\quitter.gif"));
        quitter.setToolTipText("Quitter le diporama");
        quitter.addActionListener(this);
        
        bouttons.add(precedent);
        //bouttons.add(play);
        bouttons.add(suivant);
        bouttons.add(quitter);      
    }
    
    private void afficheImage(ModeleImage mi){
        if (vI != null) cont.remove(vI);
        this.setTitle("Pikasial - Diporama : " + mi.getNom());
        vI = new VueImage(mi);
        int x, y, xS, yS, larg = mi.getLargeur(), haut = mi.getHauteur();
        /*
        double coef = larg / haut;
        
        if (larg > 900){
            haut = (int) (coef / 900);
        }else if (haut > 800){
            larg = (int) (coef * 800);
        }*/
        vI = new VueImage(mi, larg, haut);
                
        xS = Toolkit.getDefaultToolkit().getScreenSize().width;
        yS = Toolkit.getDefaultToolkit().getScreenSize().height; 
        
        x = (xS - larg) / 2;
        y = (yS - haut) / 2;
        
        vI.setBounds(x, y, haut, haut);
        vI.repaint();
        vI.validate();
        cont.add(vI);
        cont.repaint();
        cont.validate();
        this.repaint();
        this.validate();
    }
    
    public void playDiapo(){
        index = (index + 1) % array.size();
        ModeleImage mi = (ModeleImage) array.get(index);
        afficheImage(mi);
    }
 
    
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        
        if (source == suivant){
            index = (index + 1) % array.size();
            ModeleImage mi = (ModeleImage) array.get(index);
            afficheImage(mi);
        }
        else if (source == precedent){
            index--;
            if (index < 0) index = array.size() - 1;
            ModeleImage mi = (ModeleImage) array.get(index);
            afficheImage(mi);
        }
        else if (source == play){
            if (play_pause == true) {
                play_pause = false;
                play.setToolTipText("Jouer en continu");
                play.setIcon(new ImageIcon("image\\play.gif"));
                play.validate();
                if (thread == null){
                    Runnable run = new monThread(this);
                    thread = new Thread(run);
                    thread.start();
                }
            }
            else {
                play_pause = true;
                play.setToolTipText("Arrêter le diaporama");
                play.setIcon(new ImageIcon("image\\pause.gif"));
                play.validate();
                if (thread != null){
                    thread.interrupt();
                    thread = null;
                }
            }
            
            if (play_pause == true){
                
                
            }else{
                
            }
        }
        else if (source == quitter){
            this.dispose();
        }      
    }
    
    
    
    class monThread implements Runnable{
        VueDiaporama vd;

        monThread(VueDiaporama vdtmp) {
            vd = vdtmp;
        }

        
        public void run() {
            try {
                for (;;) {
                    vd.playDiapo();
                    vd.repaint();
                    vd.validate();
                    Thread.sleep(2000);
                }
            } catch (InterruptedException e) {
                return;
            }   
        }
    }
    
    
    public static void main(String[] args){
        Repertoire rep = new Repertoire("C:\\Documents and Settings\\mySpiRoU\\Bureau\\test");
        VueDiaporama diapo = new VueDiaporama(rep.getImages());
    }
}















